<?php

namespace App\Containers\AppSection\Data\Models;

use App\Containers\AppSection\File\Models\File;
use App\Containers\AppSection\User\Models\User;
use App\Ship\Parents\Models\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;

/**
 * Данные компании
 *
 * @property int $id Id
 * @property int $user_id Владелец
 * @property string $name Наименование компании
 * @property string $ogrn ОГРН
 * @property string $inn ИНН
 * @property string $legal_address Юридический адрес
 * @property string $actual_address Фактический адрес
 * @property bool $address_matches Адреса совпадают
 * @property Carbon $created_at Время создания
 * @property Carbon $updated_at Время обновления
 *
 * @property User $user Владелец
 * @property File[] $charters Устав
 */
class Company extends Model
{
    use HasJsonRelationships;

    protected $fillable = [
        'user_id',
        'name',
        'ogrn',
        'inn',
        'legal_address',
        'actual_address',
        'address_matches',
        'charters'
    ];

    protected $casts = [
        'charters' => 'json',
    ];

    protected $dates = [
        'created_at',
        'updated_at',
    ];

    protected $table = 'company_data';
    /**
     * A resource key to be used in the serialized responses.
     */
    protected string $resourceKey = 'Company';

    /**
     * Владелец
     * @return BelongsTo User
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id')->withTrashed();
    }


    /**
     * Устав
     * @return BelongsToJson Files
     */
    public function charters(): belongsToJson
    {
        return $this->belongsToJson(File::class, 'charters');
    }
}
